package github.leavesczy.reactivehttpsamples.ui.addbridge.func

import android.content.Context
import androidx.fragment.app.FragmentActivity
import com.alibaba.fastjson.JSON
import com.blankj.utilcode.util.CollectionUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.StringUtils
import com.blankj.utilcode.util.ToastUtils
import github.leavesczy.reactivehttpsamples.core.mode.BridgeRecordItem
import github.leavesczy.reactivehttpsamples.ui.addbridge.BridgeAddActivity
import github.leavesczy.reactivehttpsamples.util.exception.BusException
import github.leavesczy.reactivehttpsamples.widget.view.BridgeCheckBoxItem
import github.leavesczy.reactivehttpsamples.widget.view.BridgeEditButtonItem
import github.leavesczy.reactivehttpsamples.widget.view.BridgeEditItem
import github.leavesczy.reactivehttpsamples.widget.view.BridgeSpanItem
import github.leavesczy.reactivehttpsamples.widget.view.BridgeSpinnerItem
import java.util.Objects

interface BridgeFunction {
    fun refreshBridgeDetail(itemData: BridgeRecordItem){}
    fun  refreshBridgeType(){}

    fun dealRequestParam(mapParent: MutableMap<String,Any>){}

    fun  addTextFieldParam(map: MutableMap<String,Any>, item: BridgeEditItem, mapKey:String, tipMsg:String,necessary:Boolean=true){
        val value = item.editTextStr
        if(StringUtils.isEmpty(value)){
            if(necessary){
                throw BusException("请输入$tipMsg")
            }else{
                return
            }
        }
        map.put(mapKey,value)
    }
    fun  addTextButtonFieldParam(map: MutableMap<String,Any>, item: BridgeEditButtonItem, mapKey:String, tipMsg:String,necessary:Boolean=true){
        val value = item.editTextStr
        if(StringUtils.isEmpty(value)){
            if(necessary){
                throw BusException("${tipMsg}未处理")
            }else{
                return
            }
        }
        map.put(mapKey,value)
    }
    fun  addCheckBoxParam(map: MutableMap<String,Any>, item: BridgeCheckBoxItem, mapKey:String){
        val value = item.checkBoxChecked

        map.put(mapKey,if(value)"1" else "0")
    }
    fun  addSpinnerParam(map: MutableMap<String,Any>, item: BridgeSpinnerItem, mapKey:String, tipMsg:String,necessary:Boolean=true){
        val value = item.select
        if(Objects.isNull(value)){
            if(necessary){
                throw BusException("请选择$tipMsg")
            }else{
                return
            }
        }
        map.put(mapKey,value.value)
    }

    fun  addSpanParam(map: MutableMap<String,Any>, item: BridgeSpanItem, mapKey:String, tipMsg:String){
        val value = item.spanData
        if(CollectionUtils.isEmpty(value)){
            throw BusException("请设置$tipMsg")
        }
        if(value.size!=item.childSize()){
            throw BusException("$tipMsg 未设置完成")
        }
        map.put(mapKey,value.toString())
    }

    fun dealTextField(item: BridgeEditItem,value:String?,editable:Boolean){
        if(StringUtils.isEmpty(value)){
            return
        }
        item.editTextStr=value
        item.setEditTextEnable(editable)
    }
    fun dealTextButtonField(item: BridgeEditButtonItem,value:String?,editable:Boolean){
        if(StringUtils.isEmpty(value)){
            return
        }
        item.editTextStr=value
        item.setEditTextEnable(editable)
    }

    fun dealSpinner(item: BridgeSpinnerItem,value:String?,editable:Boolean){
        if(StringUtils.isEmpty(value)){
            return
        }
        item.setSelectValue(value)
        item.setSpinnerEnable(editable)
    }
    fun dealCheckBox(item: BridgeCheckBoxItem,value: String?,editable: Boolean){
        if(StringUtils.isEmpty(value)){
            return
        }
        item.checkBoxChecked=StringUtils.equals("1",value)
        item.setCheckBoxEnable(editable)
    }

    fun dealSpan(item: BridgeSpanItem,value: String?,editable: Boolean){
        if(StringUtils.isEmpty(value)){
            return
        }
        val parseArray = JSON.parseArray(value, String::class.java)
        item.setSpanData(parseArray)
        item.setSpanEditable(editable)
    }

    fun getForbiddenEdit(context: FragmentActivity?):Boolean{
        if (context is BridgeAddActivity){
            return context.getForbiddenEdit()
        }
        return false
    }

    fun getBridgeCode(context: FragmentActivity?):String?{
        if (context is BridgeAddActivity){
            return context.getBridgeCode()
        }
        return null
    }

    fun checkText(item: BridgeEditItem,oldValue:String?){
        val value = item.editTextStr
        if(!StringUtils.equals(value,oldValue)){
            throw BusException("内容有变动")
        }
    }
    fun checkTextButton(item: BridgeEditButtonItem,oldValue:String?){
        val value = item.editTextStr
        if(!StringUtils.equals(value,oldValue)){
            throw BusException("内容有变动")
        }
    }
    fun checkSpinner(item: BridgeSpinnerItem,oldValue: String?){
        val value = item.select
        if(null==value){
            return
        }
        if(!StringUtils.equals(value.value,oldValue)){
            throw BusException("内容有变动")
        }
    }

    fun checkSpan(item: BridgeSpanItem,oldValue: String?){
        if(!StringUtils.equals(item.spanData.toString(),oldValue)){
            throw BusException("内容有变动")
        }
    }

    fun check()
}